import Taro from '@tarojs/taro'
import $http from '../../utils/http';

const AUTH_TOKEN: string = 'auth_token';

const auth = (model: Function) => ({
  token: Taro.getStorageSync<string>(AUTH_TOKEN),
  setToken(token: string) {
    if (token) Taro.setStorageSync(AUTH_TOKEN, token);
    else Taro.removeStorageSync(AUTH_TOKEN);
    model({ token });
  },
  getTokenByRemote() {
    const formData = {
      client_id: 'mobile_web',
      client_secret: 'FanDaApp',
      grant_type: 'mobile_password_web',
    };
    $http.get(`${process.env.REACT_APP_LOGIN_URL}/oauth/token`, formData)
      .then((res: any) => {
        console.log('success', res);
      })
      .catch((err: Error) => {
        console.log('fail', err);
      });
  },
});

export default auth;
